package com.study.scheduled.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Service
public class DelayService
{
    private static final Log log = LogFactory.getLog(DelayService.class);
    int i = Runtime.getRuntime().availableProcessors();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public void delay(String param)
    {
        log.info("----------------------------------->开始进入定时任务" + date());
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(i);
        MyTask task = new MyTask(param);
        if(!StringUtils.isEmpty(param)){
            pool.schedule(task,10, TimeUnit.SECONDS);
            pool.shutdown();
        }
    }

    public String date()
    {
        return sdf.format(new Date());
    }
}
